package com.scf.loanmanage.rabbitmq;

import com.scf.loanmanage.configuration.RabbitmqUtil;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class BusinessSender {
    @Resource
    private AmqpTemplate amqpTemplate;

    //发送消息
    public void send(int delayTime, RabbitmqUtil rabbitmqUtil){

        System.out.println("发送"+new Date());
        //队列名字
        //内容

            //参数一是交换机的名字 参数二是路由KEY
        amqpTemplate.convertAndSend("延迟交换机", "死的路由",rabbitmqUtil, new MessagePostProcessor(){
                //该方法会再发送之前执行
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    //设置过期时间
                    message.getMessageProperties().setDelay(delayTime);
                    //返回message
                    return message;
                }
            });


    }


}
